<?php

$action=$_REQUEST['action'];
switch($action){
    case 'createNewTheme':
        $mainTitle1=$_REQUEST['mainTitle1'];
        $mainTitle2=$_REQUEST['mainTitle2'];
        $mainTitle3=$_REQUEST['mainTitle3'];
        $mainTitle4=$_REQUEST['mainTitle4'];
        $mainTitle5=$_REQUEST['mainTitle5'];
        $column_d=$_REQUEST['column_d'];
        $custom_hot=$_REQUEST['custom_hot'];
        $thumbimg[]=$_REQUEST['thumb_img'];
        $navpic=$_FILES['upfile5'];
        // $upfile=$_FILES['upfile'];
        $account=$_SESSION['account'];
        $dir="upload/".$account;
        if(!is_dir($dir)){
            mkdir($dir);
        }
        // if($upfile){
            // for($i=0;$i<count($upfile['name']);$i++){
                // if($upfile['name'][$i]){						
                // $filename=time().$strPol[rand(0,61)].$strPol[rand(0,61)];//用时间戳和随机数来改写图片名称
                // $houzui=strrchr($upfile['name'][$i],'.');							
                // $fulldir[$i]="upload/".$account."/".$filename.$houzui;//完整路径
                // $upfilepath[$i]=$fulldir[$i];
                // move_uploaded_file($upfile["tmp_name"][$i],$fulldir[$i]);//上传
                // } 
            // }
        // }
        if($navpic){
            $navfn=time().$strPol[rand(0,61)].$strPol[rand(0,61)];//用时间戳和随机数来改写图片名称
            $houzui=strrchr($navpic['name'],'.');							
            $full="upload/".$account."/".$navfn.$houzui;//完整路径
            $navpath=$full;
            move_uploaded_file($navpic["tmp_name"],$full);//上传
        }
        $contents=array(
            "mainTitle1"=>"$mainTitle1",
            "mainTitle2"=>"$mainTitle2",
            "mainTitle3"=>"$mainTitle3",
            "mainTitle4"=>"$mainTitle4",
            "mainTitle5"=>"$mainTitle5",
            "columnd"=>"$column_d",
            "customhot"=>"$custom_hot",
            "upfile5"=>"$full"
        );
        
        $qq = file_get_contents('yd/lhs_sms/theme.txt');
        $q = explode(',',$qq);
        for($i=0;$i<12;$i++){
            $ww[] = explode('_',$q[$i]);
        }
        //如果上传图片为空 则用原来的图片 否则换成
        for($i=0;$i<count($thumbimg[0]);$i++){
            $contents['thumbimg'.$i]=$thumbimg[0][$i]!=='images/defaultThum.jpg' ? $thumbimg[0][$i] : $ww[$i+8][1];
            //echo $contents['thumb_img'.$i];
        }
        // print_r($contents);exit;
        $path="lhs_sms/theme.txt";
        $cont=file_get_contents($path);
        if($cont){
            file_put_contents($path,"");
        }
        $str="";
        foreach($contents as $key=>$val){
            $str.= $key.'_'.$val.',';
        }
        file_put_contents($path,$str);
        echo '{"code":100,"msg":"保存成功"}';
        break;
    case 'createNewColumn':
        // print_r($_REQUEST);exit;
		// $oldcon = file_get_contents($path);
		
        $path = 'yd/lhs_sms/column'.$_POST['custom0'].'.txt';
		$oldcon = file_get_contents($path);
		$con = explode('|',$oldcon);
        
        //检查一共要配置多少个图片块
        // for($i=0; ; $i++){
            // if(!$_POST['mainTitle0'][$i])
            // break;        
        // }
                // echo $i;
        //乘以6是每个模块都有2条文字信息和4个图片  乘以4是每个图片模块有4条信息
        for($u=0; $u<10 ;$u++){
            // if(!$_POST['mainTitle0'][$u])
            // continue; 
            $u6 = $u*6;
            $u4 = $u*4;
            $con[$u6+1] = $_POST['mainTitle0'][$u] ? $_POST['mainTitle0'][$u] : $con[$u6+1];
            $con[$u6+2] = $_POST['topic0'][$u] ? $_POST['topic0'][$u] : $con[$u6+2];
            $con[$u6+3] = $_POST['thumb_img'][$u4] ? substr($_POST['thumb_img'][$u4],3) : $con[$u6+3];
            $con[$u6+4] = $_POST['thumb_img'][$u4+1] ? substr($_POST['thumb_img'][$u4+1],3) : $con[$u6+4];
            $con[$u6+5] = $_POST['thumb_img'][$u4+2] ? substr($_POST['thumb_img'][$u4+2],3) : $con[$u6+5];
            $con[$u6+6] = $_POST['thumb_img'][$u4+3] ? substr($_POST['thumb_img'][$u4+3],3) : $con[$u6+6];       
        }
		     
        $newcon=implode('|',$con);
		// print_r($con);
        // echo $newcon;
        // exit;
        file_put_contents($path,"");
        file_put_contents($path,$newcon);
        echo '{"code":100,"msg":"保存成功"}';
        break;
        /*
        *原来的代码 留个纪念吧
        
        switch($_POST['custom0']){
			case '1':
				$i='0';			
				break;
			case '2':
				$i='25';	
				break;
			case '3':
				$i='50';	
				break;
			case '4':
				$i='75';
				break;
			case '5':
				$i='100';
				break;
			default:
				break;	
		}
		$o = (int)$i;
        
		$con[$o] = 'custom_'."$num";
		$con[$o+1] = $_POST['mainTitle0'][0] ? $_POST['mainTitle0'][0] : $con[$o+1];
		$con[$o+2] = $_POST['topic0'][0] ? $_POST['topic0'][0] : $con[$o+2];
		$con[$o+3] = $_POST['thumb_img'][0] ? $_POST['thumb_img'][0] : $con[$o+3];
		$con[$o+4] = $_POST['thumb_img'][1] ? $_POST['thumb_img'][1] : $con[$o+4];
		$con[$o+5] = $_POST['thumb_img'][2] ? $_POST['thumb_img'][2] : $con[$o+5];
		$con[$o+6] = $_POST['thumb_img'][3] ? $_POST['thumb_img'][3] : $con[$o+6];
        $con[$o+7] = $_POST['mainTitle0'][1] ? $_POST['mainTitle0'][1] : $con[$o+7];
		$con[$o+8] = $_POST['topic0'][1] ? $_POST['topic0'][1] : $con[$o+8];
		$con[$o+9] = $_POST['thumb_img'][4] ? $_POST['thumb_img'][4] : $con[$o+9];
		$con[$o+10] = $_POST['thumb_img'][5] ? $_POST['thumb_img'][5] : $con[$o+10];
		$con[$o+11] = $_POST['thumb_img'][6] ? $_POST['thumb_img'][6] : $con[$o+11];
		$con[$o+12] = $_POST['thumb_img'][7] ? $_POST['thumb_img'][7] : $con[$o+12];
        $con[$o+13] = $_POST['mainTitle0'][2] ? $_POST['mainTitle0'][2] : $con[$o+13];
		$con[$o+14] = $_POST['topic0'][2] ? $_POST['topic0'][2] : $con[$o+14];
		$con[$o+15] = $_POST['thumb_img'][8] ? $_POST['thumb_img'][8] : $con[$o+15];
		$con[$o+16] = $_POST['thumb_img'][9] ? $_POST['thumb_img'][9] : $con[$o+16];
		$con[$o+17] = $_POST['thumb_img'][10] ? $_POST['thumb_img'][10] : $con[$o+17];
		$con[$o+18] = $_POST['thumb_img'][11] ? $_POST['thumb_img'][11] : $con[$o+18];
        $con[$o+19] = $_POST['mainTitle0'][3] ? $_POST['mainTitle0'][3] : $con[$o+19];
		$con[$o+20] = $_POST['topic0'][3] ? $_POST['topic0'][3] : $con[$o+20];
		$con[$o+21] = $_POST['thumb_img'][12] ? $_POST['thumb_img'][12] : $con[$o+21];
		$con[$o+22] = $_POST['thumb_img'][13] ? $_POST['thumb_img'][13] : $con[$o+22];
		$con[$o+23] = $_POST['thumb_img'][14] ? $_POST['thumb_img'][14] : $con[$o+23];
		$con[$o+24] = $_POST['thumb_img'][15] ? $_POST['thumb_img'][15] : $con[$o+24];
		*/
		
    case 'getColumn':
        switch($_SERVER['HTTP_REFERER']){
            case 'http://yd.12301.cc/client/ishow.html':
                $i='1';
                 break;
            case 'http://yd.12301.cc/client/ihotel.html';
                $i='2';
                 break;
            case 'http://yd.12301.cc/client/icound.html':
                $i='3';
                 break;
            case 'http://yd.12301.cc/client/imeal.html':
                $i='4';
                 break;
            case 'http://yd.12301.cc/client/iservice.html':
                $i='5';
                 break;
            default:
                break;
        }
        
        $path = 'yd/lhs_sms/column'.$i.'.txt';
        $content = file_get_contents($path);
        
        $con = explode('|',$content);
        
        $o = (int)$i;
        // echo $con[$o+3];
        // exit;
        // print_r($con);
        // exit;
        $len = count($con)/6;
        // echo $len;
        $arr = array();
        for($u=0; $u<$len; $u++){
            $u6= $u*6;
            $arr[$u] = array(
                'title' => $con[$u6+1],
                'desc' => $con[$u6+2],
                'simg' => array(
                                        'src' => $con[$o+3]                                       
                                        ) ,
                'bimg' => $con[$u6+3],
                'mimg' => array(
                                        $con[$u6+3],
                                        $con[$u6+4],
                                        $con[$u6+5],
                                        $con[$u6+6],                                          
                                        )                
            );
            $arr[$u]['sing']['dir']=(($u%2)==0) ? 'down':'up';
        }
        print_r(json_encode($arr));
        /*
        *以前的代码 
        $arr = array(
            array(
                'title' => $con[$o+1],
                'desc' => $con[$o+2],
                'simg' => array(
                                        'dir' => 'down',
                                        'src' => $con[$o+3]                                       
                                        ) ,
                'bimg' => $con[$o+3],
                'mimg' => array(
                                        $con[$o+3],
                                        $con[$o+4],
                                        $con[$o+5],
                                        $con[$o+6],                                          
                                        )
                    ),
            array(
                'title' => $con[$o+7],
                'desc' => $con[$o+8],
                'simg' => array(
                                        'dir' => 'up',
                                        'src' => $con[$o+9]                                       
                                        ) ,
                'bimg' => $con[$o+9],
                'mimg' => array(
                                        $con[$o+9],
                                        $con[$o+10],
                                        $con[$o+11],
                                        $con[$o+12],                                          
                                        )
                    ),
            array(
                'title' => $con[$o+13],
                'desc' => $con[$o+14],
                'simg' => array(
                                        'dir' => 'down',
                                        'src' => $con[$o+15]                                       
                                        ) ,
                'bimg' => $con[$o+15],
                'mimg' => array(
                                        $con[$o+15],
                                        $con[$o+16],
                                        $con[$o+17],
                                        $con[$o+18],                                          
                                        )
                    ),
            array(
                'title' => $con[$o+19],
                'desc' => $con[$o+20],
                'simg' => array(
                                        'dir' => 'up',
                                        'src' => $con[$o+21]                                       
                                        ) ,
                'bimg' => $con[$o+21],
                'mimg' => array(
                                        $con[$o+21],
                                        $con[$o+22],
                                        $con[$o+23],
                                        $con[$o+24],                                          
                                        )
                    )
        );
        */
        // print_r($arr);
        // exit;
    default:
        break;
        
}
?>